package com.mrent.web.controller.pay;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.Alipay;
import com.mrent.entity.GoodsOrderInfo;
import com.mrent.entity.ManagerPayment;
import com.mrent.entity.Order;
import com.mrent.entity.OrderGoods;
import com.mrent.entity.Tenpay;
import com.mrent.service.AdminPaymentService;
import com.mrent.service.GoodsOrderService;
import com.mrent.service.ManagerPaymentService;
import com.mrent.service.OrderGoodsService;
import com.mrent.service.OrderService;
import com.mrent.service.Page;
import com.mrent.util.ConfigHelper;

@Controller
public class PayController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private AdminPaymentService adminPaymentService;
	
	@Autowired
	private GoodsOrderService goodsOrderService;
	@Autowired
	private OrderGoodsService orderGoodsService;
	
	@Autowired
	private ManagerPaymentService managerPaymentService;
	
	
	/**
	 * 支付方式选择首页.
	 * @return
	 */
	@RequestMapping("/pay/index")
	public String index(String orderNo,Integer orderType,ModelMap model){
		model.addAttribute("orderNo", orderNo);
		model.addAttribute("orderType", orderType);
		Page page = new Page();
		List<AdminPayment> payments = adminPaymentService.listPayment(page);
		model.addAttribute("payments", payments);
		return "payment/index";
	}
	
	/**
	 * 选择支付方式后的提交.
	 * @return
	 */
	@RequestMapping("/pay/go")
	public String goPay(Long payId,String orderNo,Integer orderType,ModelMap model){
		AdminPayment adminPayment = adminPaymentService.getPayment(payId);
		StringBuffer sb = new StringBuffer(adminPayment.getPayUrl());
		sb.append("?orderNo=");
		sb.append(orderNo);
		sb.append("&orderType=");
		sb.append(orderType);
		sb.append("&payId=");
		sb.append(payId);
		return "redirect:" + sb.toString();
	}
	
	
	/**
	 * 支付宝支付.
	 * @param orderId 订单ID.
	 * @param orderType 订单类型(1:租店订单,2:购物订单)
	 * @return
	 */
	@RequestMapping("/pay/alipay")
	public String alipay(HttpServletRequest request,Long payId,String orderNo,Integer orderType,ModelMap model){
		if(orderType.intValue() == 1){
			Order order = orderService.getOrder(orderNo);
			if(order != null){
				String body = "申请租拍吧订单支付,天数:"+order.getOrderRequestDayNum();
				String subject = "申请租购吧订单支付";
				String price = order.getOrderPrice().toString();
				String quantity = order.getOrderRequestDayNum().toString();
				String show_url = "www.google.cn";
				String discount = "0.0";
				String out_trade_no = order.getOrderNo();
				String seller_email = ConfigHelper.getPropertyValue("alipay.seller_email");
				String logistics_type  = "EMS";
				String logistics_fee = "0";
				String return_url = "/index";
				Alipay alipay = new Alipay(body,subject,price,quantity,show_url,discount,out_trade_no,seller_email,logistics_type,logistics_fee,return_url);
				
				model.addAttribute("alipay", alipay);
			}
		}else if(orderType.intValue() == 2){
			
		}
		return "payment/alipay/index";
	}
	
	/**
	 * 财付通支付.
	 * 
	 * @param orderNo
	 * @param orderType
	 * @param model
	 * @return
	 */
	@RequestMapping("/pay/tenpay")
	public String tenpay(HttpServletRequest request,Long payId,String orderNo,Integer orderType,ModelMap model){
		AdminPayment adminPayment = adminPaymentService.getPayment(payId);
		String notifyURL = "http://"+ request.getServerName() + ":" +request.getServerPort() + adminPayment.getNotifyUrl();
		String showURL = "http://" + request.getServerName() + ":" +request.getServerPort() + adminPayment.getShowUrl();
		//申请租店类的支付
		if(orderType.intValue() == 1){
			Order order = orderService.getOrder(orderNo);
			if(order != null){
				String body = "租店支付";
				String subject = "租店支付";
				String price = order.getOrderPrice().toString();
				String quantity = order.getOrderRequestDayNum().toString();
				price = new BigDecimal(price).multiply(new BigDecimal(quantity)).multiply(new BigDecimal(100)).toPlainString();
				String out_trade_no = order.getOrderNo();
				String show_url = showURL;
				String return_url = notifyURL;
				
				Tenpay tenpay = new Tenpay();
				tenpay.setChnid(ConfigHelper.getPropertyValue("tenpay.chnid"));
				tenpay.setKey(ConfigHelper.getPropertyValue("tenpay.key"));
				tenpay.setMch_name(subject);
				tenpay.setMch_desc(body);
				tenpay.setMch_price(price);
				tenpay.setMch_returl(return_url);
				tenpay.setMch_type("2");
				tenpay.setMch_vno(out_trade_no);
				tenpay.setNeed_buyerinfo("2");
				tenpay.setSeller(ConfigHelper.getPropertyValue("tenpay.chnid"));
				tenpay.setShow_url(show_url);
				tenpay.setTransport_desc("");
				tenpay.setTransport_fee("0");
				model.addAttribute("tenpay", tenpay);
			}
		}else{//商品类支付.
			GoodsOrderInfo goodsOrder = goodsOrderService.getGoodsOrder(orderNo);
			ManagerPayment managerPayment = managerPaymentService.getByPayIdAndManager(goodsOrder.getShoperName(), payId);
			if(managerPayment == null){
				model.addAttribute("Error", "店主尚未设置财付通支付方式");
				return "payment/tenpay/index";
			}
			if(goodsOrder != null){
				StringBuffer subject = new StringBuffer();
				List<OrderGoods> orderGoods = orderGoodsService.listOrderGoods(orderNo);
				if(orderGoods.size() > 0){
					String goodsName = orderGoods.get(0).getGoodsName();
					if(goodsName.length() >5){
						goodsName = StringUtils.left(goodsName, 5);
					}
					subject.append(goodsName);
				}
				String body = "订单支付";
				subject.append("-");
				subject.append("商品订单");
				
				System.out.println("orderAmount:"+goodsOrder.getOrderAmount().multiply(new BigDecimal(100)));
				String price = goodsOrder.getOrderAmount().multiply(new BigDecimal(100)).toPlainString();
				String out_trade_no = goodsOrder.getOrderSn();
				String show_url = showURL;
				String return_url = notifyURL;
				
				Tenpay tenpay = new Tenpay();
				tenpay.setChnid(ConfigHelper.getPropertyValue("tenpay.chnid"));
				tenpay.setKey(ConfigHelper.getPropertyValue("tenpay.key"));
				tenpay.setMch_name(subject.toString());
				tenpay.setMch_desc(body);
				tenpay.setMch_price(price);
				tenpay.setMch_returl(return_url);
				tenpay.setMch_type("1");
				tenpay.setMch_vno(out_trade_no);
				tenpay.setNeed_buyerinfo("2");
				tenpay.setSeller(managerPayment.getAccount());
				tenpay.setShow_url(show_url);
				tenpay.setTransport_desc("");
				tenpay.setTransport_fee("0");
				model.addAttribute("tenpay", tenpay);
				
			}
		}
		return "payment/tenpay/index";
	}
	
	/**
	 * 网银支付.
	 * @param orderId
	 * @param orderType
	 * @param model
	 * @return
	 */
	@RequestMapping("/pay/chinablank")
	public String chinaBlank(HttpServletRequest request,Long payId,String orderNo,Integer orderType,ModelMap model){
		return null;
	}

}
